﻿using System;
using THANHTUNGO.Portal.Components.UI;
using System.Web.UI;
using System.Data;
using THANHTUNGO.Portal.Modules.NEWS.Components;
using THANHTUNGO.Portal.Components.Security;

namespace THANHTUNGO.Portal.Modules.NEWS
{
    public enum ModulePages
    {
        Item,
        Detail,
        CategoryManager,
        ItemManager
    }

    public partial class Main : PortalModuleControl
    {
        private void loadChildControl()
        {
            string m_baseDir = "~/Modules/NEWS/";
            string pageQuery = "Page" + this.ModuleID;
            string pageName = Request.QueryString[pageQuery];

            ModulePages page;
            if (pageName == null)
            {
                page = ModulePages.Item;
            }
            else
            {
                page = (ModulePages)Enum.Parse(typeof(ModulePages), Request.QueryString[pageQuery], true);
            }

            string src = string.Format("{0}Controls/{1}.ascx", m_baseDir, page);
            if (src.IndexOf("/Mod_") >= 0)
                src = src.Replace("/Mod_", "/Mod/");
            if (src.IndexOf("/Admin_") >= 0)
                src = src.Replace("/Admin_", "/Admin/");
            if (src.IndexOf("/Help_") >= 0)
                src = src.Replace("/Help_", "/Help/");

            YAFPage ctl = (YAFPage)this.LoadControl(src);
            ctl.ParentControl = this;
            ContentPane.Controls.Add(ctl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ModuleTitle.AddUrl = string.Format("~/Modules/NEWS/Pages/ItemEdit.aspx?tabindex={0}&mid={1}&itemid=0", this.TabIndex, this.ModuleID);
            }

            LiteralControl literal = new LiteralControl("");

            literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
            literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='MainMenuItem'>» Trang nhất</a></td></tr>", YAFUrlBuilder.GetLink(true, this.ModuleID, ModulePages.Item));
            literal.Text += "</table>";

            if (PortalSecurity.HasManagerPermission(this.ModuleID))
            {
                literal.Text += "<table border='0' cellpadding='6' cellspacing='0' style='width: 100%'><tr><td class='MainMenuGroup' width='100%'>Management</td></tr></table>";
                literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='MainMenuItem'>» Item</a></td></tr>", YAFUrlBuilder.GetLink(true, this.ModuleID, ModulePages.ItemManager));
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='MainMenuItem'>» Category</a></td></tr>", YAFUrlBuilder.GetLink(true, this.ModuleID, ModulePages.CategoryManager));
                literal.Text += "</table>";
            }

            DataSet ds = Category.SelectAll(this.ModuleID);
            DataRow[] rootRows = ds.Tables[0].Select("ParentID = 0");

            foreach (DataRow row in rootRows)
            {
                literal.Text += "<table border='0' cellpadding='6' cellspacing='0' style='width: 100%'><tr><td class='MainMenuGroup' width='100%'>" + row["Name"] + "</td></tr></table>";
                DataRow[] childRows = ds.Tables[0].Select("ParentID = " + row["ID"]);
                if (childRows.Length > 0)
                {
                    literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
                    foreach (DataRow childRow in childRows)
                    {
                        literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='MainMenuItem'>» {1}</a></td></tr>", YAFUrlBuilder.GetLink(true, this.ModuleID, ModulePages.Item, "categoryid={0}&categoryname={1}", childRow["ID"], Server.UrlEncode(childRow["Name"].ToString())), childRow["Name"]);
                    }
                    literal.Text += "</table>";
                }
            }
            
            LeftPane.Controls.Add(literal);

            this.loadChildControl();
        }
    }
}